package chu.dejian.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sam.blemodule.R;
import com.sam.map.ShowALocationActivity;
import com.sam.widget.button.StateBitmapImageButton;

import java.util.ArrayList;
import java.util.List;

import chu.dejian.entity.MeasureMessageEntity;

public class AttentionMessageAdapter extends BaseAdapter implements Filterable{
    private LayoutInflater inflater;
    private List<MeasureMessageEntity> messageList;
    private MeasureMessageFilter messageFilter;
    protected Context context;

    public AttentionMessageAdapter(Context context,List<MeasureMessageEntity> messageList) {
        this.inflater = LayoutInflater.from(context);
        this.messageList = messageList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return messageList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.row_attention_message, null, false);
            viewHolder.msgLayout = (LinearLayout) convertView.findViewById(R.id.message_layout);
            viewHolder.attendeeName = (TextView) convertView.findViewById(R.id.attendee_name);
            viewHolder.messageTime = (TextView) convertView.findViewById(R.id.message_time);
            viewHolder.locationBtn = (StateBitmapImageButton) convertView.findViewById(R.id.location);
            viewHolder.sysValue = (TextView) convertView.findViewById(R.id.systolic_value);
            viewHolder.diaValue = (TextView) convertView.findViewById(R.id.diastolic_value);
            viewHolder.pulseValue = (TextView) convertView.findViewById(R.id.pulse_value);
            viewHolder.desContent = (TextView) convertView.findViewById(R.id.des_content);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final MeasureMessageEntity messageEntity = messageList.get(position);
        int[] blockDraws = {R.drawable.color_block_ff687b,R.drawable.color_block_f5a623,R.drawable.color_block_7ed321};
        @ColorInt int[] colors = {context.getResources().getColor(R.color.message_red_bg),context.getResources().getColor(R.color.message_orange_bg),context.getResources().getColor(R.color.message_green_bg)};
        Drawable bd = context.getResources().getDrawable(blockDraws[messageEntity.level - 1]);
        bd.setBounds(0,0,bd.getMinimumWidth(),bd.getMinimumHeight());
        viewHolder.messageTime.setCompoundDrawables(bd,null,null,null);
        viewHolder.sysValue.setTextColor(colors[messageEntity.level - 1]);
        viewHolder.diaValue.setTextColor(colors[messageEntity.level - 1]);
        viewHolder.pulseValue.setTextColor(colors[messageEntity.level - 1]);

        viewHolder.attendeeName.setText(messageEntity.attendee);
        viewHolder.messageTime.setText(messageEntity.timeStr);
        viewHolder.sysValue.setText(messageEntity.systolic+"");
        viewHolder.diaValue.setText(messageEntity.diastolic+"");
        viewHolder.pulseValue.setText(messageEntity.pulse+"");
        viewHolder.desContent.setText(messageEntity.message);
        if(messageEntity.latitude <= 0 || messageEntity.longitude <= 0) viewHolder.locationBtn.setVisibility(View.INVISIBLE);
        else viewHolder.locationBtn.setVisibility(View.VISIBLE);
        viewHolder.locationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowALocationActivity.showLocation(context,messageEntity.latitude,messageEntity.longitude);
            }
        });
        return convertView;
    }


    @Override
    public Filter getFilter() {
        if (messageFilter == null) {
            messageFilter = new MeasureMessageFilter(messageList);
        }
        return messageFilter;
    }

    private class MeasureMessageFilter extends Filter {

        private List<MeasureMessageEntity> original;

        public MeasureMessageFilter(List<MeasureMessageEntity> list) {
            this.original = list;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                results.values = original;
                results.count = original.size();
            } else {
                List<MeasureMessageEntity> mList = new ArrayList<MeasureMessageEntity>();
                for (MeasureMessageEntity message : original) {
                    if (message.getForMatchStr().contains(constraint)) {
                        mList.add(message);
                    }
                }
                results.values = mList;
                results.count = mList.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            messageList = (List<MeasureMessageEntity>)results.values;
            notifyDataSetChanged();
        }
    }

    private class ViewHolder {
        LinearLayout msgLayout;
        TextView attendeeName,messageTime,sysValue,diaValue,pulseValue,desContent;
        StateBitmapImageButton locationBtn;
    }

}
